main, status: Factor out deployment printing into helper
authorColin Walters <walters@verbum.org>
Mon, 19 Mar 2018 13:46:20 +0000 (09:46 -0400)
committerAtomic Bot <atomic-devel@projectatomic.io>
Mon, 19 Mar 2018 17:15:27 +0000 (17:15 +0000)
Prep for staged deployments; they won't be in the primary deployment
list, and we want to print them first.

Also pull in some code from rpm-ostree for the red/bold bits and use
that tree-wide.

Update submodule: libglnx

Closes: #1504
Approved by: jlebon

libglnx
src/ostree/main.c
src/ostree/ostree-trivial-httpd.c
src/ostree/ot-admin-builtin-status.c
src/ostree/ot-main.h

diff --git a/libglnx b/libglnx
index 96b1fd9578b3d6ff2d8e0707068f5ef450eba98c..0c82203cd459a35cc3f471e3205355e9fb79160f 160000 (submodule)
--- a/libglnx
+++ b/libglnx
@@ -1 +1 @@
-Subproject commit 96b1fd9578b3d6ff2d8e0707068f5ef450eba98c
+Subproject commit 0c82203cd459a35cc3f471e3205355e9fb79160f
index ddf531d17b57518700a8abee60d4ebe8375cedfd..9ed0f88010416ac01f5e30cd418996af27c07462 100644 (file)
@@ -141,15 +141,10 @@ main (int    argc,
 
   if (error != NULL)
     {
-      int is_tty = isatty (1);
-      const char *prefix = "";
-      const char *suffix = "";
-      if (is_tty)
-        {
-          prefix = "\x1b[31m\x1b[1m"; /* red, bold */
-          suffix = "\x1b[22m\x1b[0m"; /* bold off, color reset */
-        }
-      g_printerr ("%serror: %s%s\n", prefix, suffix, error->message);
+      g_printerr ("%s%serror:%s%s %s\n",
+                  ot_get_red_start (), ot_get_bold_start (),
+                  ot_get_bold_end (), ot_get_red_end (),
+                  error->message);
     }
 
   return ret;
index 377de525590f67b24a0b4701f6782bb82b3aacd7..5da5a6abc29b84641973c5380911f8860a15274e 100644 (file)
@@ -680,15 +680,10 @@ main (int    argc,
 
   if (!run (argc, argv, cancellable, &error))
     {
-      int is_tty = isatty (1);
-      const char *prefix = "";
-      const char *suffix = "";
-      if (is_tty)
-        {
-          prefix = "\x1b[31m\x1b[1m"; /* red, bold */
-          suffix = "\x1b[22m\x1b[0m"; /* bold off, color reset */
-        }
-      g_printerr ("%serror: %s%s\n", prefix, suffix, error->message);
+      g_printerr ("%s%serror:%s%s %s\n",
+                  ot_get_red_start (), ot_get_bold_start (),
+                  ot_get_bold_end (), ot_get_red_end (),
+                  error->message);
       return 1;
     }
 
index a2e24ef34bcfdc0cd8bdca52401ac4231923c727..096155c688ce2da7c0e260847351d3eff081c8f5 100644 (file)
@@ -27,6 +27,7 @@
 #include "ot-admin-builtins.h"
 #include "ot-admin-functions.h"
 #include "ostree.h"
+#include "libglnx.h"
 
 #include <glib/gi18n.h>
 
@@ -61,13 +62,116 @@ deployment_get_gpg_verify (OstreeDeployment *deployment,
   return gpg_verify;
 }
 
+
+static gboolean
+deployment_print_status (OstreeSysroot    *sysroot,
+                         OstreeRepo       *repo,
+                         OstreeDeployment *deployment,
+                         gboolean          is_booted,
+                         gboolean          is_pending,
+                         gboolean          is_rollback,
+                         GCancellable     *cancellable,
+                         GError          **error)
+{
+  const char *ref = ostree_deployment_get_csum (deployment);
+
+  /* Load the backing commit; shouldn't normally fail, but if it does,
+   * we stumble on.
+   */
+  g_autoptr(GVariant) commit = NULL;
+  (void)ostree_repo_load_variant (repo, OSTREE_OBJECT_TYPE_COMMIT, ref,
+                                  &commit, NULL);
+  g_autoptr(GVariant) commit_metadata = NULL;
+  if (commit)
+    commit_metadata = g_variant_get_child_value (commit, 0);
+
+  const char *version = NULL;
+  const char *source_title = NULL;
+  if (commit_metadata)
+    {
+      (void) g_variant_lookup (commit_metadata, OSTREE_COMMIT_META_KEY_VERSION, "&s", &version);
+      (void) g_variant_lookup (commit_metadata, OSTREE_COMMIT_META_KEY_SOURCE_TITLE, "&s", &source_title);
+    }
+
+  GKeyFile *origin = ostree_deployment_get_origin (deployment);
+
+  const char *deployment_status = "";
+  if (is_pending)
+    deployment_status = " (pending)";
+  else if (is_rollback)
+    deployment_status = " (rollback)";
+  g_print ("%c %s %s.%d%s\n",
+           is_booted ? '*' : ' ',
+           ostree_deployment_get_osname (deployment),
+           ostree_deployment_get_csum (deployment),
+           ostree_deployment_get_deployserial (deployment),
+           deployment_status);
+  if (version)
+    g_print ("    Version: %s\n", version);
+
+  OstreeDeploymentUnlockedState unlocked = ostree_deployment_get_unlocked (deployment);
+  switch (unlocked)
+    {
+    case OSTREE_DEPLOYMENT_UNLOCKED_NONE:
+      break;
+    default:
+      g_print ("    %s%sUnlocked: %s%s%s\n", ot_get_red_start (), ot_get_bold_start (),
+               ostree_deployment_unlocked_state_to_string (unlocked),
+               ot_get_bold_end (), ot_get_red_end ());
+    }
+  if (ostree_deployment_is_pinned (deployment))
+    g_print ("    Pinned: yes\n");
+  if (!origin)
+    g_print ("    origin: none\n");
+  else
+    {
+      g_autofree char *origin_refspec = g_key_file_get_string (origin, "origin", "refspec", NULL);
+      if (!origin_refspec)
+        g_print ("    origin: <unknown origin type>\n");
+      else
+        g_print ("    origin refspec: %s\n", origin_refspec);
+      if (source_title)
+        g_print ("    `- %s\n", source_title);
+    }
+
+  if (deployment_get_gpg_verify (deployment, repo))
+    {
+      g_autoptr(GString) output_buffer = g_string_sized_new (256);
+      /* Print any digital signatures on this commit. */
+
+      g_autoptr(GError) local_error = NULL;
+      g_autoptr(OstreeGpgVerifyResult) result =
+        ostree_repo_verify_commit_ext (repo, ref, NULL, NULL,
+                                       cancellable, &local_error);
+
+      /* G_IO_ERROR_NOT_FOUND just means the commit is not signed. */
+      if (g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
+        {
+          g_clear_error (&local_error);
+          return TRUE;
+        }
+      else if (local_error != NULL)
+        {
+          g_propagate_error (error, g_steal_pointer (&local_error));
+          return FALSE;
+        }
+
+      const guint n_signatures = ostree_gpg_verify_result_count_all (result);
+      for (guint jj = 0; jj < n_signatures; jj++)
+        {
+          ostree_gpg_verify_result_describe (result, jj, output_buffer, "    GPG: ",
+                                             OSTREE_GPG_SIGNATURE_FORMAT_DEFAULT);
+        }
+
+      g_print ("%s", output_buffer->str);
+    }
+
+  return TRUE;
+}
+
 gboolean
 ot_admin_builtin_status (int argc, char **argv, OstreeCommandInvocation *invocation, GCancellable *cancellable, GError **error)
 {
-  const int is_tty = isatty (1);
-  const char *red_bold_prefix = is_tty ? "\x1b[31m\x1b[1m" : "";
-  const char *red_bold_suffix = is_tty ? "\x1b[22m\x1b[0m" : "";
-
   g_autoptr(GOptionContext) context = g_option_context_new ("");
 
   g_autoptr(OstreeSysroot) sysroot = NULL;
@@ -98,98 +202,13 @@ ot_admin_builtin_status (int argc, char **argv, OstreeCommandInvocation *invocat
       for (guint i = 0; i < deployments->len; i++)
         {
           OstreeDeployment *deployment = deployments->pdata[i];
-          const char *ref = ostree_deployment_get_csum (deployment);
-
-          /* Load the backing commit; shouldn't normally fail, but if it does,
-           * we stumble on.
-           */
-          g_autoptr(GVariant) commit = NULL;
-          (void)ostree_repo_load_variant (repo, OSTREE_OBJECT_TYPE_COMMIT, ref,
-                                          &commit, NULL);
-          g_autoptr(GVariant) commit_metadata = NULL;
-          if (commit)
-            commit_metadata = g_variant_get_child_value (commit, 0);
-
-          const char *version = NULL;
-          const char *source_title = NULL;
-          if (commit_metadata)
-            {
-              (void) g_variant_lookup (commit_metadata, OSTREE_COMMIT_META_KEY_VERSION, "&s", &version);
-              (void) g_variant_lookup (commit_metadata, OSTREE_COMMIT_META_KEY_SOURCE_TITLE, "&s", &source_title);
-            }
-
-          GKeyFile *origin = ostree_deployment_get_origin (deployment);
-
-          const char *deployment_status = "";
-          if (deployment == pending_deployment)
-            deployment_status = " (pending)";
-          else if (deployment == rollback_deployment)
-            deployment_status = " (rollback)";
-          g_print ("%c %s %s.%d%s\n",
-                   deployment == booted_deployment ? '*' : ' ',
-                   ostree_deployment_get_osname (deployment),
-                   ostree_deployment_get_csum (deployment),
-                   ostree_deployment_get_deployserial (deployment),
-                   deployment_status);
-          if (version)
-            g_print ("    Version: %s\n", version);
-
-          OstreeDeploymentUnlockedState unlocked = ostree_deployment_get_unlocked (deployment);
-          switch (unlocked)
-            {
-            case OSTREE_DEPLOYMENT_UNLOCKED_NONE:
-              break;
-            default:
-              g_print ("    %sUnlocked: %s%s\n", red_bold_prefix,
-                       ostree_deployment_unlocked_state_to_string (unlocked),
-                       red_bold_suffix);
-            }
-          if (ostree_deployment_is_pinned (deployment))
-            g_print ("    Pinned: yes\n");
-          if (!origin)
-            g_print ("    origin: none\n");
-          else
-            {
-              g_autofree char *origin_refspec = g_key_file_get_string (origin, "origin", "refspec", NULL);
-              if (!origin_refspec)
-                g_print ("    origin: <unknown origin type>\n");
-              else
-                g_print ("    origin refspec: %s\n", origin_refspec);
-              if (source_title)
-                g_print ("    `- %s\n", source_title);
-            }
-
-          if (deployment_get_gpg_verify (deployment, repo))
-            {
-              g_autoptr(GString) output_buffer = g_string_sized_new (256);
-              /* Print any digital signatures on this commit. */
-
-              g_autoptr(GError) local_error = NULL;
-              g_autoptr(OstreeGpgVerifyResult) result =
-                ostree_repo_verify_commit_ext (repo, ref, NULL, NULL,
-                                               cancellable, &local_error);
-
-              /* G_IO_ERROR_NOT_FOUND just means the commit is not signed. */
-              if (g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
-                {
-                  g_clear_error (&local_error);
-                  continue;
-                }
-              else if (local_error != NULL)
-                {
-                  g_propagate_error (error, g_steal_pointer (&local_error));
-                  return FALSE;
-                }
-
-              const guint n_signatures = ostree_gpg_verify_result_count_all (result);
-              for (guint jj = 0; jj < n_signatures; jj++)
-                {
-                  ostree_gpg_verify_result_describe (result, jj, output_buffer, "    GPG: ",
-                                                     OSTREE_GPG_SIGNATURE_FORMAT_DEFAULT);
-                }
-
-              g_print ("%s", output_buffer->str);
-            }
+          if (!deployment_print_status (sysroot, repo, deployment,
+                                        deployment == booted_deployment,
+                                        deployment == pending_deployment,
+                                        deployment == rollback_deployment,
+                                        cancellable,
+                                        error))
+            return FALSE;
         }
     }
 
index c5ffeb7f30ffab9a6f15610996eeff486e5dd79c..ac75118cafd29c8ef29355ef0621b3eea9c56814 100644 (file)
@@ -91,3 +91,18 @@ gboolean ostree_ensure_repo_writable (OstreeRepo *repo, GError **error);
 void ostree_print_gpg_verify_result (OstreeGpgVerifyResult *result);
 
 gboolean ot_enable_tombstone_commits (OstreeRepo *repo, GError **error);
+
+/* Copied from rpm-ostree's rpmostree-libbuiltin.h */
+#define TERM_ESCAPE_SEQUENCE(type,seq)          \
+  static inline const char* ot_get_##type (void) { \
+    if (glnx_stdout_is_tty ())                  \
+      return seq;                               \
+    return "";                                  \
+  }
+
+TERM_ESCAPE_SEQUENCE(red_start,  "\x1b[31m")
+TERM_ESCAPE_SEQUENCE(red_end,    "\x1b[22m")
+TERM_ESCAPE_SEQUENCE(bold_start, "\x1b[1m")
+TERM_ESCAPE_SEQUENCE(bold_end,   "\x1b[0m")
+
+#undef TERM_ESCAPE_SEQUENCE